<?php
/**
 * Created by IntelliJ IDEA
 * Author: 张伯发
 * Date  : 2019/11/22
 * Time  : 17:09
 */

namespace App\Service\Members;


use App\Dto\Cache\MembersMessageCacheDTO;
use App\Model\Entity\Members;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Redis\Pool;
use Xiaoproject\Common\Base\BaseService;
use Xiaoproject\Common\Language\L;

/**
 * Class MembersMessageService
 * @author 张伯发 2019/11/22 17:09
 * @Bean()
 */
class MembersMessageService extends BaseService
{
    private const MEMBERS_TOKEN_KEY = 'members_token_key:';

    private const MEMBERS_TOKEN_TIMEOUT = 86400 * 7; // 7天失效

    /**
     * @Inject()
     * @var Pool
     */
    protected $redis;

    /**
     * 设置缓存
     * @param Members $members
     * @return string
     * @author 张伯发 2019/11/22 18:47
     */
    public function setCacheMembersMessage(Members $members) : string
    {
        $cacheDto = new MembersMessageCacheDTO();
        $cacheDto->setUsername($members->getUsername());
        $cacheDto->setNickname($members->getNickname());
        $cacheDto->setHeadIcon($members->getHeadIcon());
        $cacheDto->setUserId($members->getId());

        $token = $this->membersToken($members);
        $cacheKey = $this->createMemberTokenCacheKey($token);
        $this->redis->set($cacheKey, $cacheDto, self::MEMBERS_TOKEN_TIMEOUT);
        return $token;
    }

    /**
     * @param $token
     * @return MembersMessageCacheDTO|null
     * @author 张伯发 2019/11/22 17:55
     */
    public function getCacheMembersMessage(string $token):  ?MembersMessageCacheDTO
    {
        if (empty($token)) return null;
        $cacheKey = $this->createMemberTokenCacheKey($token);

        $result = $this->redis->get($cacheKey);
        if ($result === false) return null;
        return $result;
    }

    /**
     *
     * @param string $token
     * @return bool
     * @author 张伯发 2019/11/23 11:14
     */
    public function delCacheMembersMessage(string $token) : bool
    {
        if (empty($token)){
            $this->setMessage(L::get('members.token_error'));
            return false;
        }

        $token = $this->createMemberTokenCacheKey($token);

        $exists = $this->redis->exists($token);
        if ($exists === false) {
            $this->setMessage(L::get('members.token_error'));
            return false;
        }

        $result = $this->redis->del($token);
        if (!$result) {
            $this->setMessage(L::get('members.token_error'));
            return false;
        }
        return true;
    }


    /**
     *
     * @param $token
     * @return string
     * @author 张伯发 2019/11/22 17:54
     */
    private function createMemberTokenCacheKey(string $token) :string
    {
        return self::MEMBERS_TOKEN_KEY . $token;
    }


    /**
     * @param Members $members
     * @return string
     * @author 张伯发 2019/11/22 17:50
     */
    private function membersToken(Members $members) : string
    {
        $message = $members->getUsername()
            . $members->getPassword()
            . $members->getNickname()
            . $members->getHeadIcon();
        return strtoupper(md5(md5($message ) . time(). getRandom(6)));
    }


}
